package com.bringway.fm.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import com.bringway.fm.dao.AttDao;
import com.bringway.fm.domian.Att;
import com.bringway.framework.dao.BaseJdbcDao;
import com.bringway.framework.domain.SysAttechment;

import net.sf.json.JSONObject;
/*
* @author 作者 xuef
* @version 创建时间：2015年9月14日 上午10:51:13
* 类说明
*/
@Repository
public class AttDaoImpl extends BaseJdbcDao implements AttDao{
	
	
	private class AttMapper implements RowMapper{

		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Att att = new Att();
			att.setId((Long)(rs.getObject("ID")));
			att.setOperUserId(rs.getString("OPER_USER_ID"));
			att.setOperUserName(rs.getString("OPER_USER_NAME"));
			att.setOperDate(rs.getTimestamp("OPER_DATE"));
			att.setComment(rs.getString("COMMENT"));
			return att;
		}
		
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public List<Map<String, Object>>  queryAttList(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		String gyh = params.optString("gyh");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		sql.append(" SELECT ID, OPER_USER_ID, OPER_USER_NAME, DATE_FORMAT(OPER_DATE,'%Y-%m-%d') OPER_DATE, COMMENT ");
		sql.append(" ,SHARE_ROLE_IDS,SHARE_ROLE_NAMES,TOP_LEVEL ");
		sql.append(" FROM T_FM_ATT ");
		sql.append(" WHERE 1=1 AND STATE= '00' AND OPER_USER_ID=:OPER_USER_ID ");
		paramMap.put("OPER_USER_ID", gyh);
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND OPER_DATE >= :startDate");
			paramMap.put("startDate",startDate);
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND OPER_DATE <= :endDate");
			paramMap.put("endDate",endDate);
		}
		sql.append(" ORDER BY TOP_LEVEL DESC,OPER_DATE DESC,ID DESC ");
		if(0 != limit){
			sql.append(" LIMIT :START,:LIMIT");
		}
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(),paramMap);
	}

	@SuppressWarnings("deprecation")
	@Override
	public int getAttCount(JSONObject params) {
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		String gyh = params.optString("gyh");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		sql.append(" SELECT COUNT(*) CT ");
		sql.append(" FROM T_FM_ATT ");
		sql.append(" WHERE 1=1 AND STATE= '00' AND OPER_USER_ID=:OPER_USER_ID ");
		paramMap.put("OPER_USER_ID", gyh);
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND OPER_DATE >= :startDate");
			paramMap.put("startDate",startDate);
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND OPER_DATE <= :endDate");
			paramMap.put("endDate",endDate);
		}
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}

	@Override
	public String saveAtt(JSONObject params) {
		StringBuffer sql = new StringBuffer();
		KeyHolder keyHolder = new GeneratedKeyHolder();
		String uploadOperUserId = params.optString("uploadOperUserId");
		String uploadOperUserName = params.optString("uploadOperUserName");
		String roleId = params.optString("roleId");
		String roleName = params.optString("roleName");
		String comment = params.optString("comment");
		String shareRoleIds = params.optString("shareRoleIds");
		String shareRoleNames = params.optString("shareRoleNames");
		
		sql.append(" INSERT INTO d_ccb_fm.t_fm_att(OPER_USER_ID,OPER_USER_NAME,OPER_DATE,COMMENT ");
		sql.append(" ,OPER_USER_ROLE_ID,OPER_USER_ROLE_NAME,SHARE_ROLE_IDS,SHARE_ROLE_NAMES) ");
		sql.append(" VALUES (:OPER_USER_ID,:OPER_USER_NAME,now(),:COMMENT ");
		sql.append(" ,:OPER_USER_ROLE_ID,:OPER_USER_ROLE_NAME,:SHARE_ROLE_IDS,:SHARE_ROLE_NAMES) ");
		SqlParameterSource parameters = new MapSqlParameterSource()
				.addValue("OPER_USER_ID", uploadOperUserId)
                .addValue("OPER_USER_NAME", uploadOperUserName)
                .addValue("COMMENT", comment)
                .addValue("OPER_USER_ROLE_ID", roleId)
                .addValue("OPER_USER_ROLE_NAME", roleName)
                .addValue("SHARE_ROLE_IDS", shareRoleIds)
                .addValue("SHARE_ROLE_NAMES", shareRoleNames);
		this.getNamedParameterJdbcTemplate().update(sql.toString(), parameters, keyHolder);
		return keyHolder.getKey().toString();
	}

	@Override
	public void deleteAtt(JSONObject params) {
		
		
		String bizId = params.optString("bizId");
		String gyh = params.optString("gyh");
		String username = params.optString("username");
		
		
		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE T_FM_ATT ");
		sql.append(" SET DELETE_USER_ID = :DELETE_USER_ID ");
		sql.append(" ,DELETE_USER_NAME = :DELETE_USER_NAME ");
		sql.append(" ,DELETE_DATE = now() ");
		sql.append(" ,STATE = '01' ");
		sql.append(" WHERE ID = :ID ");
		SqlParameterSource parameters = new MapSqlParameterSource()
				.addValue("ID", bizId)
				.addValue("DELETE_USER_ID", gyh)
				.addValue("DELETE_USER_NAME", username);
		this.getNamedParameterJdbcTemplate().update(sql.toString(), parameters);
	}

	@Override
	public void topAtt(JSONObject params) {
		String bizId = params.optString("bizId");
		String topLevel = params.optString("topLevel");
		topLevel = "1";
		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE T_FM_ATT ");
		sql.append(" SET TOP_LEVEL = :TOP_LEVEL ");
		sql.append(" WHERE ID = :ID ");
		SqlParameterSource parameters = new MapSqlParameterSource()
				.addValue("ID", bizId)
				.addValue("TOP_LEVEL", topLevel);
		this.getNamedParameterJdbcTemplate().update(sql.toString(), parameters);
	}

	@Override
	public void unTopAtt(JSONObject params) {
		String bizId = params.optString("bizId");
		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE T_FM_ATT ");
		sql.append(" SET TOP_LEVEL = '0' ");
		sql.append(" WHERE ID = :ID ");
		SqlParameterSource parameters = new MapSqlParameterSource()
				.addValue("ID", bizId);
		this.getNamedParameterJdbcTemplate().update(sql.toString(), parameters);
	}
	

}
